﻿/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */

using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    public class TriggetInspector : RecordEventBaseInspector
    {
        private TriggerEvent triggerData;

        public override void Show(VisualElement root)
        {
            this.root = root;
            itemStyle = ActionEditorInspector.currentTrackItemStyle;
            data = itemStyle.eventData;
            triggerData = data as TriggerEvent;

            ShowRecord();
            ShowTrackName();
            ShowStartIndex();
            ShowDurationIndex();

            ShowTag();
            ShowCenter();
            if (triggerData is BoxTriggerEvent)
            {
                ShowSize();
                ShowRotate();
                ShowScale();

            }
            else if (triggerData is SphereTriggerEvent)
            {
                ShowSphereRadius();
            }
            else if (triggerData is SectorTriggerEvent)
            {
                ShowSectorRadius();
                ShowSectorHeight();
                ShowSectorAngle();
            }
        }

        protected void ShowTag()
        {
            TextField nameField = new TextField("Tag");
            nameField.value = triggerData.tag;
            nameField.RegisterCallback<FocusOutEvent>(p =>
            {
                if (triggerData.tag != nameField.value)
                {
                    TUndo.RecordObject(triggerData,"TriggerEvent name change");
                    triggerData.tag = nameField.value;
                    itemStyle.ResetView(); 
                } 
            });
            root.Add(nameField);
        }

        private void ShowCenter()
        {
            Vector3Field positon = new Vector3Field("位置");
            positon.value = triggerData.position;
            positon.RegisterValueChangedCallback(p =>
            {
                if (triggerData.position != positon.value)
                {
                    triggerData.position = positon.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(positon);
        }

        private void ShowRotate()
        {
            BoxTriggerEvent box = triggerData as BoxTriggerEvent;
            Vector3Field rotate = new Vector3Field("旋转");
            rotate.value = box.rotate;
            rotate.RegisterValueChangedCallback(p =>
            {
                if (box.rotate != rotate.value)
                {
                    box.rotate = rotate.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(rotate);
        }

        private void ShowScale()
        {
            BoxTriggerEvent box = triggerData as BoxTriggerEvent;
            Vector3Field scale = new Vector3Field("缩放");
            scale.value = box.scale;
            scale.RegisterValueChangedCallback(p =>
            {
                if (box.scale != scale.value)
                {
                    box.scale = scale.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(scale);
        }

        private void ShowSize()
        {
            BoxTriggerEvent box = triggerData as BoxTriggerEvent;
            Vector3Field positon = new Vector3Field("大小");
            positon.value = box.size;
            positon.RegisterValueChangedCallback(p =>
            {
                if (box.size != positon.value)
                {
                    box.size = positon.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(positon);
        }


        private void ShowSphereRadius()
        {
            SphereTriggerEvent sphere = triggerData as SphereTriggerEvent;
            FloatField radius = new FloatField("半径");
            radius.value = sphere.radius;
            radius.RegisterValueChangedCallback(p =>
            {
                if (sphere.radius != radius.value)
                {
                    sphere.radius = radius.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(radius);
        }

        private void ShowSectorRadius()
        {
            SectorTriggerEvent sector = triggerData as SectorTriggerEvent;
            FloatField radius = new FloatField("半径");
            radius.value = sector.radius;
            radius.RegisterValueChangedCallback(p =>
            {
                if (sector.radius != radius.value)
                {
                    sector.radius = radius.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(radius);
        }

        private void ShowSectorHeight()
        {
            SectorTriggerEvent sector = triggerData as SectorTriggerEvent;
            FloatField radius = new FloatField("高");
            radius.value = sector.height;
            radius.RegisterValueChangedCallback(p =>
            {
                if (sector.height != radius.value)
                {
                    sector.height = radius.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(radius);
        }

        private void ShowSectorAngle()
        {
            SectorTriggerEvent sector = triggerData as SectorTriggerEvent;
            FloatField radius = new FloatField("角度");
            radius.value = sector.angle;
            radius.RegisterValueChangedCallback(p =>
            {
                if (sector.angle != radius.value)
                {
                    sector.angle = radius.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(radius);
        }
    }
}
